package day13.XianCheng;

public class Lian09Myrunnable implements Runnable{
    private static int ticked = 100;

    @Override
    public void run() {
        while (true) {
            if ("窗口一".equals(Thread.currentThread().getName())) {
//同步方法
                boolean result = synchronizedMthod();
                if (result) {
                    break;
                }
            } else if ("窗口二".equals(Thread.currentThread().getName())) {
//                同步代码块
                synchronized (Lian09Myrunnable.class){
                    if (ticked==0){
                        break;
                    }else {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        ticked--;
                        System.out.println(Thread.currentThread().getName()+"在卖票,剩余"+ticked);
                    }
                }
            }
        }
    }

    private static synchronized boolean synchronizedMthod() {
        if (ticked == 0) {
            return true;
        } else {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticked--;
            System.out.println(Thread.currentThread().getName()+"在卖票,剩余"+ticked);
            return false;
        }
    }
}
